package com.zonta.finance.serivce.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zonta.finance.common.exception.BizException;
import com.zonta.finance.common.service.TokenService;
import com.zonta.finance.domain.dto.AdminDTO;
import com.zonta.finance.domain.form.RoleBindMenuForm;
import com.zonta.finance.domain.po.SysMenu;
import com.zonta.finance.domain.po.SysRoleBindMenu;
import com.zonta.finance.mapper.SysMenuMapper;
import com.zonta.finance.mapper.SysRoleBindMenuMapper;
import com.zonta.finance.serivce.ISysRoleBindMenuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class SysRoleBindMenuServiceImpl  extends ServiceImpl<SysRoleBindMenuMapper, SysRoleBindMenu> implements ISysRoleBindMenuService {
    private final TokenService<AdminDTO> tokenService;
    private final TransactionTemplate transactionTemplate;

    /**
     * 角色绑定菜单列表
     * @param form
     * @return
     */
    @Override
    public boolean roleBindMenu(RoleBindMenuForm form) {
        if (CollectionUtils.isEmpty(form.getBindMenuIds()) && CollectionUtils.isEmpty(form.getUnBindMenuIds())) {
            throw new BizException("您未做任何更改，不能提交绑定");
        }
        List<SysRoleBindMenu> sysRoleBindMenuList = new ArrayList<>();
        if (CollUtil.isNotEmpty(form.getBindMenuIds())){
            checkRoleBindMenuIds(form.getRoleId(), form.getBindMenuIds());
            for (Integer menuId : form.getBindMenuIds()) {
                SysRoleBindMenu sysRoleBindMenu = new SysRoleBindMenu();
                sysRoleBindMenu.setSysRoleId(form.getRoleId());
                sysRoleBindMenu.setSysMenuId(menuId);
                sysRoleBindMenu.setMemberId(tokenService.getThreadLocalUserId());
                sysRoleBindMenu.setUpdateMemberId(tokenService.getThreadLocalUserId());
                sysRoleBindMenu.initDefault();
                sysRoleBindMenuList.add(sysRoleBindMenu);
            }
        }
        transactionTemplate.execute((transactionStatus) -> {
            if (!CollectionUtils.isEmpty(form.getBindMenuIds())) {
                saveBatch(sysRoleBindMenuList);
            }
            if (!CollectionUtils.isEmpty(form.getUnBindMenuIds())){
                LambdaUpdateWrapper<SysRoleBindMenu> wrapper = new LambdaUpdateWrapper<>();
                wrapper.eq(SysRoleBindMenu::getSysRoleId,form.getRoleId())
                        .in(SysRoleBindMenu::getSysMenuId,form.getUnBindMenuIds());
                int delete = baseMapper.delete(wrapper);

            }
            return true;
    });
        return true;
    }
    private void checkRoleBindMenuIds(Integer roleId, List<Integer> bindMenuIds) {
        LambdaQueryWrapper<SysRoleBindMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleBindMenu::getSysRoleId,roleId);
        wrapper.in(SysRoleBindMenu::getMemberId,bindMenuIds);
        if (count(wrapper)>0){
            throw new BizException("角色不要重复绑定菜单");
        }
    }

    /**
     * 查询绑定的资源列表
     * @param roleId
     * @return
     */
    @Override
    public List<Integer> listBindMenuIdByRoleId(int roleId) {
        LambdaQueryWrapper<SysRoleBindMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleBindMenu::getSysRoleId,roleId);
        List<SysRoleBindMenu> sysRoleBindMenus = list(wrapper);
        return sysRoleBindMenus.stream().map(SysRoleBindMenu::getSysMenuId).collect(Collectors.toList());
    }
}
